/*
 * Copyright (c) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import router from '@system.router';
import okio from 'okio';
import okhttp from 'okhttp_ohos';

var byteStringObj = new okio.ByteString.ByteString('Hello world');
export default {
    data: {
        title: "",
        fileUri: '/data/data/com.openharmony.ohos.okioapplication/cache/test.txt',
        writeUtfValue: "",
        readUtfValue: "",
        encodeBase64Value:"",
        client: new okhttp.OkHttpClient.Builder().setConnectTimeout(10000).build()
    },
    onInit() {
    },
    onComplete: function (result) {
        if (result.response) {
            this.status = result.response.responseCode;
        }
        if (result.response.result)
        this.content = result.response.result;
        else
        this.content = JSON.stringify(result.response);

        console.info("onComplete -> Status : " + this.status);
        console.info("onComplete -> Content : " + JSON.stringify(this.content));
    },

    onError: function (error) {
        this.status = "";
        this.content = error.data;
        console.info("onError -> Error : " + this.content);
    },

    getStringUtfValue(e) {
        this.writeUtfValue = e.text;
    },
    performWriteUtf8() {
        var buffer = new okio.Buffer();

        buffer.writeUtf8(this.writeUtfValue);
        this.readUtfValue = buffer.readUtf8();
    },
    encodeBase64() {
        let encodeBase64 = byteStringObj.encodeUtf8('Hello world').Base64();
        this.encodeBase64Value = JSON.stringify(encodeBase64);
    },
    getWriteValue(e) {
        this.writeInputValue = e.text;
    },
    writeFile() {
        var sink = new okio.Sink(this.fileUri);
        var isAppend = false;

        sink.write(this.writeInputValue,isAppend);
    },

    okhttpEncodeBase64() {

        let encodeBase64 = byteStringObj.encodeUtf8('Hello world').Base64();
        this.encodeBase64Value = JSON.stringify(encodeBase64);

        var request = new okhttp.Request.Builder()
            .url("http://121.244.27.58:12272/echoserver")
            .POST(okhttp.RequestBody.create(this.encodeBase64Value))
            .addHeader("Content-Type", "application/json")
            .build();

        this.client.newCall(request).enqueue(this.onComplete, this.onError);
    },

    onDeodeComplete: function (result) {
        if (result.response) {
            this.status = result.response.responseCode;
        }
        if (result.response.result)
        this.content = result.response.result;
        else
        this.content = JSON.stringify(result.response);

        let decodeBase64 = byteStringObj.decodeBase64(this.content);
        this.decodeBase64Value = JSON.stringify(decodeBase64);

        console.info("onDeodeComplete -> Status : " + this.status);
        console.info("onDeodeComplete -> Content : " + this.content);
        console.info("onDeodeComplete -> decodeBase64Value : " + this.decodeBase64Value);
    },

    onDecodeError: function (error) {
        this.status = "";
        this.content = error.data;
        console.info("onDecodeError -> Error : " + this.content);
    },

    okhttpDecodeBase64() {

        var request = new okhttp.Request.Builder()
            .url("http://121.244.27.58:12272/decodebase64")
            .POST(okhttp.RequestBody.create(this.encodeBase64Value))
            .addHeader("Content-Type", "application/json")
            .build();

        this.client.newCall(request).enqueue(this.onDeodeComplete, this.onDecodeError);
    },

    okhttpEncodeHex() {

        let encodeHex = byteStringObj.encodeUtf8('Hello world').Hex();
        this.encodeHexValue = JSON.stringify(encodeHex);

        var request = new okhttp.Request.Builder()
            .url("http://121.244.27.58:12272/echoserver")
            .POST(okhttp.RequestBody.create(this.encodeHexValue))
            .addHeader("Content-Type", "application/json")
            .build();

        this.client.newCall(request).enqueue(this.onComplete, this.onError);
    },

    encodeMD5Hex() {

        let encodeHex = byteStringObj.encodeUtf8('Hello world test test').md5().Hex();
        this.encodemd5HexValue = JSON.stringify(encodeHex);

        var request = new okhttp.Request.Builder()
            .url("http://121.244.27.58:12272/echoserver")
            .POST(okhttp.RequestBody.create(this.encodemd5HexValue))
            .addHeader("Content-Type", "application/json")
            .build();

        this.client.newCall(request).enqueue(this.onComplete, this.onError);
    },

    onHexDecodeComplete: function (result) {
        if (result.response) {
            this.status = result.response.responseCode;
        }
        if (result.response.result)
        this.content = result.response.result;
        else
        this.content = JSON.stringify(result.response);

        let decodehex = byteStringObj.decodeHex(this.content);
        this.decodeHexValue = JSON.stringify(decodehex);

        console.info("onHexDecodeComplete -> Status : " + this.status);
        console.info("onHexDecodeComplete -> Content : " + this.content);
        console.info("onHexDecodeComplete -> decodeHexValue : " + this.decodeHexValue);
    },

    onHexDecodeError: function (error) {
        this.status = "";
        this.content = error.data;
        console.info("onHexDecodeError -> Error : " + this.content);
    },

    okhttpDecodeHex() {

        var request = new okhttp.Request.Builder()
            .GET("http://121.244.27.58:12272/decodehex")
            .addHeader("Content-Type", "application/json")
            .build();


        this.client.newCall(request).enqueue(this.onDeodeComplete, this.onDecodeError);
    },

    showOkHttpIndex() {
        router.push({
            uri: 'pages/index/index'
        })
    }
}
